home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / toswinsc.zoo / filbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-27  |  1.4 KB  |  86 lines

  1. /*
  2.  * Copyright 1992 Eric R. Smith. All rights reserved.
  3.  * Redistribution is permitted only if the distribution
  4.  * is not for profit, and only if all documentation
  5.  * (including, in particular, the file "copying")
  6.  * is included in the distribution in unmodified form.
  7.  * THIS PROGRAM COMES WITH ABSOLUTELY NO WARRANTY, NOT
  8.  * EVEN THE IMPLIED WARRANTIES OF MERCHANTIBILITY OR
  9.  * FITNESS FOR A PARTICULAR PURPOSE. USE AT YOUR OWN
  10.  * RISK.
  11.  */
  12. #include <osbind.h>
  13. #include <stdlib.h>
  14. #include "filbuf.h"
  15.  
  16. FILBUF *
  17. FBopen(name)
  18.     char *name;
  19. {
  20.     FILBUF *f;
  21.  
  22.     f = malloc( sizeof(FILBUF) );
  23.     if (!f) return f;
  24.  
  25.     f->fd = Fopen(name, 0);
  26.     if (f->fd < 0) {
  27.         free(f);
  28.         return 0;
  29.     }
  30.     f->nbytes = 0;
  31.     return f;
  32. }
  33.  
  34. void
  35. FBclose(f)
  36.     FILBUF *f;
  37. {
  38.     Fclose(f->fd);
  39.     free(f);
  40. }
  41.  
  42. int
  43. FBgetchar(f)
  44.     FILBUF *f;
  45. {
  46.     int r;
  47.  
  48.     if (f->nbytes <= 0) {
  49.         r = Fread(f->fd, (long)FILBUFSIZ, f->buf);
  50.         if (r <= 0) return -1;    /* EOF */
  51.         f->nbytes = r;
  52.         f->ptr = f->buf;
  53.     }
  54.     f->nbytes--;
  55.     r = *f->ptr;
  56.     f->ptr++;
  57.     return r;
  58. }
  59.  
  60. char *
  61. FBgets(f, buf, siz)
  62.     FILBUF *f;
  63.     char *buf;
  64.     int siz;
  65. {
  66.     char *obuf;
  67.     int c;
  68.  
  69.     obuf = buf;
  70.     if (siz <= 0) return 0;
  71.  
  72.     --siz;        /* for the trailing NULL */
  73.  
  74.     for(;;) {
  75.         c = FBgetchar(f);
  76.         if (c == '\n') break;
  77.         if (c < 0) return 0;
  78.         if (siz && c != '\r') {
  79.             *buf++ = c;
  80.             --siz;
  81.         }
  82.     }
  83.     *buf++ = 0;
  84.     return obuf;
  85. }
  86.